﻿/*
************************************************************
CreateDate  *  Author        *  Content
2008-08-04  *  Donne.Yang  *  Query data and execute data
************************************************************
NO   *  UpdateDate   *  Author        *  Content

*************************************************************
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections;
using System.Configuration;
using System.Windows.Forms;

namespace Smg.DAL
{
    public class DBException
    {

        public DBException()
        {
            if (Configuration.ProjectName == null || Configuration.LogPath == null)
            {
                Configuration.ProjectName = ConfigurationSettings.AppSettings["ProjectName"];
                Configuration.LogPath = ConfigurationSettings.AppSettings["LogPath"];
            }
        }

        /// <summary>
        /// 设置Log保存路径
        /// </summary>
        /// <returns>Log保存路径</returns>
        private static string GetPath()
        {
            string strPath = Application.StartupPath;

            if (Configuration.LogPath != "" && Configuration.LogPath != null && Directory.Exists(Configuration.LogPath) == true)
            {
                strPath = Configuration.LogPath;
            }

            strPath += @"\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
            return strPath;
        }

        /// <summary>
        /// 记录异常日志
        /// </summary>
        /// <param name="ex">异常</param>
        /// <param name="strMsg">自定义异常说明</param>
        public void SaveException(Exception ex, string strMsg)
        {
            string strPath = GetPath();
            StreamWriter streamWriter = null;

            try
            {
                streamWriter = new StreamWriter(strPath, true);
                streamWriter.WriteLine("\n");
                streamWriter.WriteLine("Project Name:" + Configuration.ProjectName);
                streamWriter.WriteLine("Error Description:" + strMsg);
                streamWriter.WriteLine("Error Message:" + ex.Message.ToString());
                streamWriter.WriteLine("DateTime:" + DateTime.Now.ToString());
            }
            catch
            {
            }
            finally
            {
                try
                {
                    streamWriter.Close();
                }
                catch { }
            }
        }

        /// <summary>
        /// 记录异常日志及Sql命令
        /// </summary>
        /// <param name="ex">异常</param>
        /// <param name="strMsg">自定义异常说明</param>
        /// <param name="strSql">SQL语句</param>
        public void SaveException(Exception ex, string strMsg, string strSql)
        {
            string strPath = GetPath();

            StreamWriter streamWriter = null;

            try
            {
                streamWriter = new StreamWriter(strPath, true);
                streamWriter.WriteLine("\n");
                streamWriter.WriteLine("Project Name:" + Configuration.ProjectName);
                streamWriter.WriteLine("Error Description:" + strMsg);
                streamWriter.WriteLine("Error Message:" + ex.Message.ToString());
                streamWriter.WriteLine("sql Description:" + strSql);
                streamWriter.WriteLine("DateTime:" + DateTime.Now.ToString());
            }
            catch
            {
            }
            finally
            {
                try
                {
                    streamWriter.Close();
                }
                catch { }
            }
        }

        /// <summary>
        /// 记录异常日志及多条SQL
        /// </summary>
        /// <param name="ex">异常</param>
        /// <param name="strMsg">﹚自定义异常说明</param>
        /// <param name="arrSqls">SQL语句</param>
        public void SaveException(Exception ex, string strMsg, ArrayList arrSqls)
        {
            string strPath = GetPath();

            StreamWriter streamWriter = null;

            try
            {
                streamWriter = new StreamWriter(strPath, true);
                streamWriter.WriteLine("\n");
                streamWriter.WriteLine("Project Name:" + Configuration.ProjectName);
                streamWriter.WriteLine("Error Description:" + strMsg);
                streamWriter.WriteLine("Error Message:" + ex.Message.ToString());
                for (int i = 0; i < arrSqls.Count; i++)
                {
                    streamWriter.WriteLine("sql Description:" + arrSqls[i]);
                }
                streamWriter.WriteLine("DateTime:" + DateTime.Now.ToString());
            }
            catch
            {
            }
            finally
            {
                try
                {
                    streamWriter.Close();
                }
                catch { }
            }
        }

        /// <summary>
        /// 记录发邮件异常  
        /// </summary>
        /// <param name="ex">异常</param>
        /// <param name="strMsg">说明</param>
        public void SaveMailException(Exception ex, string strMsg)
        {
            string strPath = GetPath();

            StreamWriter streamWriter = null;

            try
            {
                streamWriter = new StreamWriter(strPath, true);
                streamWriter.WriteLine("\n");
                streamWriter.WriteLine("Project Name:" + Configuration.ProjectName);
                streamWriter.WriteLine("Error Description:" + strMsg);
                streamWriter.WriteLine("Error Message:" + ex.Message.ToString());
                streamWriter.WriteLine("DateTime:" + DateTime.Now.ToString());
            }
            catch
            {
            }
            finally
            {
                try
                {
                    streamWriter.Close();
                }
                catch { }
            }
        }


        /// <summary>
        /// 记录一条语句
        /// </summary>
        /// <param name="strMsg">说明</param>
        public void SaveErrorLog(string strMsg)
        {
            string strPath = GetPath();
            StreamWriter streamWriter = null;

            try
            {
                streamWriter = new StreamWriter(strPath, true);
                streamWriter.WriteLine("\n");
                streamWriter.WriteLine("Project Name:" + Configuration.ProjectName);
                streamWriter.WriteLine("Description:" + strMsg);
                streamWriter.WriteLine("DateTime:" + DateTime.Now.ToString());
            }
            catch
            {
            }
            finally
            {
                try
                {
                    streamWriter.Close();
                }
                catch { }
            }
        }
    }
}
